#include "layoutgenomeelements.h"

#include "../../VisBaseClasses/visbaseelement.h"

LayoutGenomeElements::LayoutGenomeElements(QObject *parent) :
    VisBaseLayout(parent)
{
    init();
}

LayoutGenomeElements::LayoutGenomeElements(QList< QPointer< VisBaseElement > > e, QObject *parent) :
    VisBaseLayout(parent)
{
    elementsToLayout.append(e);

    init();
}

//LayoutGenomeElements::LayoutGenomeElements()

void LayoutGenomeElements::init()
{
    startNt = 0;
    stopNt = 10000;
    startX = 0.0;
    stopX = 1200;
    ntPerPixel = (stopNt-startNt)/(stopX-startX);
}

void LayoutGenomeElements::setStartNt(float s)
{
    startNt = s;
}

void LayoutGenomeElements::setStopNt(float s)
{
    stopNt = s;
}

void LayoutGenomeElements::setStartX(float s)
{
    startX = s;
}

void LayoutGenomeElements::setStopX(float s)
{
    stopX = s;
}

float LayoutGenomeElements::getStartNt()
{
    return startNt;
}

float LayoutGenomeElements::getStopNt()
{
    return stopNt;
}

float LayoutGenomeElements::getStartX()
{
    return startX;
}

float LayoutGenomeElements::getStopX()
{
    return stopX;
}

void LayoutGenomeElements::swapStartNtAndStopNt()
{
    int temp = startNt;
    startNt = stopNt;
    stopNt = temp;
}

void LayoutGenomeElements::swapStartAndStop(int nt)
{
    int temp = startNt;
    startNt = stopNt;
    stopNt = temp;
    qDebug() << "     swapped nt: " << startNt << " " << stopNt << " nt: " << nt <<  "result x: " << map(nt) << endl;

//    int temp2 = startX;
//    startX = stopX;
//    stopX = temp2;
//    qDebug() << "     swapped x: " << startX << " " << stopX << " nt: " << nt <<  "result x: " << map(nt) << endl;
}

void LayoutGenomeElements::rotateAroundNt(int nt)
{
    float ntDistFromStart = nt - startNt;
    float ntDistFromStop = stopNt - nt;

    startNt = nt + ntDistFromStart;
    stopNt = nt - ntDistFromStop;
}


void LayoutGenomeElements::setNtPerPixel(float npp)
{
    ntPerPixel = npp;
//    float centerNt = stopNt-startNt; //hold center fixed
//    float newRange = ntPerPixel * ((float)stopX-(float)startX);
//    setStartNt( centerNt - newRange/2.0);
//    setStopNt( centerNt + newRange/2.0);
//    setStopNt( ntPerPixel*(stopX-startX)+startNt);
}

float LayoutGenomeElements::getNtPerPixel()
{
   return ntPerPixel;
}

void LayoutGenomeElements::process()
{
    //qDebug() <<"layout genome elements " << elementsToLayout.size() << endl;

    for(int i = 0 ; i < elementsToLayout.size(); i++)
    {
//        qDebug() << getGenomeElement(i)->getStart() << endl;
//        qDebug() << getGenomeElement(i)->getStop() << endl;

        GenomeElement* ge = getGenomeElement(i);
        float geneStartX = map( ge->getStart() );
        float geneStopX = map( ge->getStop() );

        //qDebug() << "       " << geneStartX << " " << geneStopX << endl;
        if( geneStartX < geneStopX )
        {
            ge->setX(geneStartX);
            ge->setWidth(geneStopX-geneStartX);
        }
        else
        {
            ge->setX(geneStopX);
            ge->setWidth(geneStartX-geneStopX);
        }
    }
    //qDebug() << "done laying out " << endl;

}

float LayoutGenomeElements::map(float nt)
{
    return ( (nt-startNt )/( stopNt-startNt ) * (stopX-startX) + startX);
}

void LayoutGenomeElements::moveNt(float deltaNt)
{
    if( startNt < stopNt )
    {
        setStartNt( startNt + deltaNt );
        setStopNt( stopNt + deltaNt );
    }
    else
    {
        setStartNt( startNt - deltaNt );
        setStopNt( stopNt - deltaNt );
    }
}

void LayoutGenomeElements::resetIfOutOfBounds()
{
//    if( getGenomeElement(0)->pos().x() > startX )
//    {
//        setStartNt(  )
//    }
}

GenomeElement* LayoutGenomeElements::getGenomeElement(int i)
{
    VisBaseElement* vbe = elementsToLayout[i];
    GenomeElement* ge = dynamic_cast< GenomeElement* > ( vbe );
    return ge;
}


